package com.neusoft.neu.fy25g01.n98.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.neusoft.neu.fy25g01.n98.entity.CommonResult;
import com.neusoft.neu.fy25g01.n98.entity.Role;
import com.neusoft.neu.fy25g01.n98.mapper.RoleMapper;
import com.neusoft.neu.fy25g01.n98.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PutMapping;

import java.util.List;

@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleMapper roleMapper;


    @Override
    public Role findById(Integer rid) {
        LambdaQueryWrapper<Role> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Role::getRid, rid);
        final var role = roleMapper.selectOne(queryWrapper);
        return role;
    }

    @Override
    public int modRole(Role role) {
        // updateById 自动启用动态SQL，忽略值为null的属性
        final int i = roleMapper.updateById(role);
        return i;
    }

    @Override
    public CommonResult addRole(Role role) {
//        数据校验
        // 解决主键问题
        role.setRid(null); // 使用主键自增
        // insert:返回影响记录的行数，主键值在实体类的主键属性中
        final int i = roleMapper.insert(role);
        if (i > 0) {
            return CommonResult.success(role.getRid());
        }
        return CommonResult.failed();


    }

    @Override
    public int delRole(Integer id) {
        final int i = roleMapper.deleteById(id);
        return i;
    }

    @Override
    public CommonResult<List<Role>> findAll() {
        List<Role> list = roleMapper.selectList(null);
        return CommonResult.success(list);
    }

    @Override
    public List<Role> findAll1() {
        LambdaQueryWrapper<Role> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Role::getRid, 1);
        wrapper.eq(Role::getStatus, 1);
        return roleMapper.selectList(wrapper);
    }
}
